﻿Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Imports DevScope.CartaoDeCidadao
Public Class frmSettings
    Inherits Form
    Public Delegate Sub UpdateSettingsHandler(sender As Object, e As UpdateSettingsEventArgs)
    Public Event UpdateSettings As UpdateSettingsHandler

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(cardReaders As List(Of String), selectedReader As String, proxy As ProxyInfo)
        Me.New()
        For Each cardReader As String In cardReaders
            ddlCardReaders.Items.Add(cardReader)
        Next

        ddlCardReaders.Text = selectedReader

        chkUseProxy.Checked = Not [String].IsNullOrEmpty(proxy.Server)
        txtAddress.Text = proxy.Server
        If proxy.Port <> 0 Then
            udPort.Value = proxy.Port
        End If
        chkUseProxy_CheckedChanged(Nothing, Nothing)

        chkUseAuthentication.Checked = Not [String].IsNullOrEmpty(proxy.Username)
        txtUsername.Text = proxy.Username
        txtPassword.Text = proxy.Password
        chkUseAuthentication_CheckedChanged(Nothing, Nothing)
    End Sub

    Private Sub chkUseProxy_CheckedChanged(sender As Object, e As EventArgs) Handles chkUseProxy.CheckedChanged
        lblAddress.Enabled = chkUseProxy.Checked
        txtAddress.Enabled = chkUseProxy.Checked
        lblPort.Enabled = chkUseProxy.Checked
        udPort.Enabled = chkUseProxy.Checked
        grpAuthentication.Enabled = chkUseProxy.Checked
    End Sub

    Private Sub chkUseAuthentication_CheckedChanged(sender As Object, e As EventArgs) Handles chkUseAuthentication.CheckedChanged
        lblUsername.Enabled = chkUseAuthentication.Checked
        txtUsername.Enabled = chkUseAuthentication.Checked
        lblPassword.Enabled = chkUseAuthentication.Checked
        txtPassword.Enabled = chkUseAuthentication.Checked
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Dim proxyInfo As New ProxyInfo()
        If chkUseProxy.Checked Then
            proxyInfo.Server = txtAddress.Text
            proxyInfo.Port = Convert.ToUInt32(udPort.Value)
            If chkUseAuthentication.Checked Then
                proxyInfo.Username = txtUsername.Text
                proxyInfo.Password = txtPassword.Text
            End If
        End If

        Dim settings As New UpdateSettingsEventArgs(ddlCardReaders.Text, proxyInfo)
        RaiseEvent UpdateSettings(Me, settings)
        Me.Close()
    End Sub

    Private Sub frmSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

Public Class UpdateSettingsEventArgs
    Inherits System.EventArgs
    Private m_selectedReader As String
    Private proxyInfo As ProxyInfo

    Public Property SelectedReader() As String
        Get
            Return Me.m_selectedReader
        End Get
        Set(value As String)


        End Set
    End Property

    Public Property ProxyConfiguration() As ProxyInfo
        Get
            Return Me.proxyInfo
        End Get
        Set(value As ProxyInfo)


        End Set
    End Property

    Public Sub New(selectedReader As String, proxyInfo As ProxyInfo)
        Me.m_selectedReader = selectedReader
        Me.proxyInfo = proxyInfo
    End Sub
End Class